package com.infolab.aims.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.validator.constraints.NotBlank;

import com.infolab.aims.common.SpecialMuLineToStringStyle;
import com.infolab.aims.common.SpecialToStringStyle;
import com.infolab.aims.common.bean.AbstractModelBean;
import com.infolab.aims.common.bean.ContactInfo;


@Entity
@Table(name="AMTB_SYSTEM_USER")
public class SystemUser extends AbstractModelBean implements Serializable {
	
	@Column(unique=true)
	private String nric;
	
	/**
	 *  @see SystemUser CONSTANT_USERTYPE
	 */
	@NotBlank(message="{error.empty}")
	private String userType;
	
	@Id
	@NotBlank(message="{error.empty}")
	private String userName;
	private String personName;
	private String password;

	private boolean superAdmin;
	
	@Column(columnDefinition="number(1,0) default 1 not null")
	private boolean active;
	
	@ManyToOne(optional=true)
	@JoinColumn(name="departmentId")
	private Department department;
	
	@ManyToOne
	@JoinColumn(name="sectionId")
	private Section section;

	private ContactInfo contactInfo;
	
	@ManyToMany
	@JoinTable(name="AMTB_SYSTEM_USER_ROLE",
	joinColumns=@JoinColumn(name="userName"),
	inverseJoinColumns=@JoinColumn(name="systemRoleId"))
	private List<SystemRole> systemRoles;
	
	@Transient
	private String departmentName;
	
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public boolean isSuperAdmin() {
		return superAdmin;
	}
	public void setSuperAdmin(boolean superAdmin) {
		this.superAdmin = superAdmin;
	}
	public Department getDepartment() {
		return department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}
	public String getNric() {
		return nric;
	}
	public void setNric(String nric) {
		this.nric = nric;
	}
	public String getUserType() {
		return userType;
	}
	public void setUserType(String userType) {
		this.userType = userType;
	}
	public String getPersonName() {
		return personName;
	}
	public void setPersonName(String personName) {
		this.personName = personName;
	}
	public boolean isActive() {
		return active;
	}
	public void setActive(boolean active) {
		this.active = active;
	}
	public ContactInfo getContactInfo() {
		return contactInfo;
	}
	public void setContactInfo(ContactInfo contactInfo) {
		this.contactInfo = contactInfo;
	}
	public List<SystemRole> getSystemRoles() {
		return systemRoles;
	}
	public void setSystemRoles(List<SystemRole> systemRoles) {
		this.systemRoles = systemRoles;
	}
	public Section getSection() {
		return section;
	}
	public void setSection(Section section) {
		this.section = section;
	}
	
	public SystemUser() {
	
	}
	
	public SystemUser(String userName,String nric,String personName,String departmentName,boolean active,String userType,String modifiedBy,Date dateModified) {
		
		this.userName=userName;
		this.nric=nric;
		this.personName=personName;
		this.departmentName=departmentName;
		this.active=active;
		this.userType=userType;
		setModifiedBy(modifiedBy);
		setDateModified(dateModified);
	}

	@Override
	public String toString() {
		return getUserName();
	}
	public String getDepartmentName() {
		return departmentName;
	}
	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}
	
	public String getAuditContentHead()
	{
		
		Department dept = department!=null?department:new Department();
		Section sec = section!=null? section:new Section();
		ContactInfo contInfo = contactInfo!=null? contactInfo:new ContactInfo();
		
		return new ToStringBuilder(this,SpecialToStringStyle.SPECIAL_STYLE)
			.append("Nric",nric)			
			.append("User Name",userName)
			.append("User Type",userType)
			.append("Person Name",personName)
			.append("SuperAdmin",superAdmin)
			.append("Active",active)
			.append("Department", (dept.getDepartmentCode()!=null?dept.getDepartmentCode():"" )
					+ " " +(dept.getDepartmentName()!=null?dept.getDepartmentName():"All Department"))
			.append("Section",(sec.getSectionCode()!=null?sec.getSectionCode():"" )
				+ " " +(sec.getSectionName()!=null?sec.getSectionName():"All Section"))
			.append("Mobile No",contInfo.getMobileNo()!=null?contInfo.getMobileNo():"")
			.append("Contact No",contInfo.getContactNo()!=null?contInfo.getContactNo():"")
			.append("Contact Address",contInfo.getContactAddress()!=null?contInfo.getContactAddress():"")
			.append("Contact Postal  Code",contInfo.getContactPostalCode()!=null?contInfo.getContactPostalCode():"")
			.append("Email",contInfo.getEmail()!=null?contInfo.getEmail():"")
			.append("Fax No",contInfo.getFaxNo()!=null?contInfo.getFaxNo():"")
			.toString();
	}
	
	public String getAuditContentHeadForUserRole()
	{
		
		Department dept = department!=null?department:new Department();
		Section sec = section!=null? section:new Section();
		
		return new ToStringBuilder(this,SpecialToStringStyle.SPECIAL_STYLE)
			.append("User Name",userName)
			.append("Department", (dept.getDepartmentCode()!=null?dept.getDepartmentCode():"" )
					+ " " +(dept.getDepartmentName()!=null?dept.getDepartmentName():"All Department"))
			.append("Section",(sec.getSectionCode()!=null?sec.getSectionCode():"" )
				+ " " +(sec.getSectionName()!=null?sec.getSectionName():"All Section"))			
			.toString();
	}
	
	public String getAuditContentDetail()
	{
		ToStringBuilder builder = new ToStringBuilder(this,SpecialMuLineToStringStyle.SPECIAL_MULINE_STYLE);
		
		if(systemRoles!=null && CollectionUtils.isNotEmpty(systemRoles))
		{
			int i = 0;
			for(SystemRole systemRole : systemRoles)
			{
				    builder.append("Role " + (++i), systemRole.getRoleName());
			}
			String str = builder.toString();
			return StringUtils.substring(str, str.indexOf("[")+1, str.lastIndexOf("]")-1);
		}	
				
		return super.getAuditContentDetail();
		
	}

	
	public String getAuditCode()
	{
		if(StringUtils.isNotEmpty(userName)){
			return userName;
		}
		return super.getAuditCode();
	}

	
	public static final String USERTYPE_SECURITY_GUARD="SG";
	public static final String USERTYPE_VISITOR="VIS";
	public static final String USERTYPE_SYSTEMUSER="SYUS";


}
